//=============================================================================
// XBus 
//-----------------------------------------------------------------------------
// File        : xbus_path.c
// Description : 
// Author      : Ismail Sabry
// Date        : 1/30/2009
//=============================================================================
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grp.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/dir.h>

#include <xbus_file.h>

#define XBUS_CMD_SIZE           256

static int is_path_exist(char *path);
static int create_path(char *path);
static int remove_path(char *path);

#define XBUS_USE_CMD_SYSEM     1

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int XBUS_init_path(char *name)
{
  char       path[XBUS_MAX_PATH];
  
  // cheek if path exist
  snprintf(path, XBUS_MAX_PATH-1, "/%s/%s/%s", XBUS_ROOT, XBUS_DIR, name);
  if( is_path_exist(path) ) {
    return -1;
  }
  
  // creat path
  snprintf(path, XBUS_MAX_PATH-1, "/%s", XBUS_ROOT);
  if( is_path_exist(path) ) {
    //printf("==> dir : %s : exite \n", path);
    snprintf(path, XBUS_MAX_PATH-1, "/%s/%s", XBUS_ROOT, XBUS_DIR);    
    if( !is_path_exist(path) ) {
      create_path(path);
    }
    
    if( is_path_exist(path) ) {
      //printf("==> dir : %s : exite \n", path);
      snprintf(path, XBUS_MAX_PATH-1, "/%s/%s/%s", XBUS_ROOT, XBUS_DIR, name);
      create_path(path);
      
      if( ! is_path_exist(path) ) {
	return -1;
      } else {
	//printf("==> dir : %s : exite \n", path);
      }
    } else {
      return -1;
    }
  }
  
  return 0;
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
int XBUS_clean_path(char *name)
{
  char       path[XBUS_MAX_PATH];
  
  snprintf(path, XBUS_MAX_PATH-1, "/%s/%s/%s", XBUS_ROOT, XBUS_DIR, name);
  if( remove_path(path) == -1) {
    return -1 ;
  }
  
  return 0;
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
static int is_path_exist(char *path)
{
  struct stat      sts;
  
  if(stat(path, &sts) != 0) {
    //printf("stat %s error (%s)\n", path, strerror(errno));
    return 0 ;
  } else {
    return 1;
  }
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
static int create_path(char *path)
{
#if XBUS_USE_CMD_SYSEM
  char cmd[XBUS_CMD_SIZE];

  snprintf(cmd, XBUS_CMD_SIZE, "mkdir %s", path);
  if( system(cmd) == -1) {
    printf("system %s error (%s)\n", cmd, strerror(errno));
    return -1;
  }
  return 0;
#else
  int   mode;
  
  mode = \
    S_IRUSR + S_IWUSR+ S_IXUSR + \
    S_IRGRP + S_IWGRP+ S_IXGRP + \
    S_IROTH + S_IXGRP;
  
  if( mkdir(path, mode) == -1 ) {
    printf("mkdir %s error (%s)\n", path, strerror(errno));
    return -1;
  }

  return 0;
#endif
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
static int remove_path(char *path)
{
#if XBUS_USE_CMD_SYSEM
  char cmd[XBUS_CMD_SIZE];

  snprintf(cmd, XBUS_CMD_SIZE, "rm -r %s", path);
  if( system(cmd) == -1) {
    printf("system %s error (%s)\n", cmd, strerror(errno));
    return -1;
  }
  return 0;
#else
  if (rmdir(path) == -1) {
    printf("rmdir %s error (%s)\n", path, strerror(errno));
    return -1;
  } 
  return 0;
#endif
}
